package com.intexh.quickandroid.module.chat.ui.messageItemView;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.intexh.quickandroid.R;
import com.intexh.quickandroid.module.chat.ChatHelper;
import com.intexh.quickandroid.module.chat.ChatManager.VoiceManager;
import com.intexh.quickandroid.module.chat.bean.ChatMessage;
import com.intexh.quickandroid.module.chat.util.FileDownload;
import com.intexh.quickandroid.module.chat.util.FileUtil;
import com.intexh.quickandroid.module.chat.util.IDownLoadListener;
import com.intexh.quickandroid.utils.LogCatUtil;
import com.intexh.quickandroid.utils.ValidateUtils;

import java.io.File;


/**
 * Created by engine on 13-12-17.
 */
public class VoiceMessageItem extends AbsChatItem implements VoiceManager.IPlayListener, IDownLoadListener {
    int postion = 0;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            findViewById(R.id.voiceMark).setVisibility(GONE);
            mMessage.setPlayed(1);
            mMessage.saveAsync();
            if (mMessage.getLocalUrl() == null) {
                LogCatUtil.e("frank","点击网络语音");
                playRemoteVoice(mMessage.getContent());
            } else {
                LogCatUtil.e("frank","点击本地语音");
                File file = new File(mMessage.getLocalUrl());
                VoiceManager.INSTANCE.playVoice(file, VoiceMessageItem.this);
            }

        }
    };
    private ImageView playImage;
    private int[] fromPlay = {R.mipmap.play_from_1, R.mipmap.play_from_2, R.mipmap.play_from_3};
    private int[] toPlay = {R.mipmap.play_to_1, R.mipmap.play_to_2, R.mipmap.play_to_3};
    private int[] targetImage;
    private int defaultImage;
    private boolean isPlaying = false;

    public VoiceMessageItem(Context context, ChatMessage message, boolean needTime) {
        this(context, message, needTime, false);
    }

    public VoiceMessageItem(Context context, ChatMessage message, boolean needTime, boolean isNearChat) {
        super(context);
        this.mNeedTime = needTime;
        this.mMessage = message;
        this.mContext = context;
        View view;
        if (ChatHelper.getInstance().getUserInfo().getUid().equals(mMessage.getUid())) {//发出去的
            view = LayoutInflater.from(context).inflate(R.layout.message_voice_to, this);
            targetImage = toPlay;
            defaultImage = R.mipmap.chat_voice_play;
        } else {//接收到的
            view =  LayoutInflater.from(context).inflate(R.layout.message_voice_from, this);
            targetImage = fromPlay;
            defaultImage = R.mipmap.chat_voice_play_from;
        }
        setLayout();
        view.findViewById(R.id.session_content).setOnClickListener(onClickListener);
    }

    private void setLayout() {
        setCommonLayout();
        if (mMessage==null)
            LogCatUtil.e("frank","显示语音消息未读："+mMessage.getPlayed());
        if (mMessage.getPlayed() == 0 && !ChatHelper.getInstance().getUserInfo().getUid().equals(mMessage.getUid())) {
            findViewById(R.id.voiceMark).setVisibility(VISIBLE);
        } else {
            findViewById(R.id.voiceMark).setVisibility(GONE);
        }
        TextView contentTextView = (TextView) findViewById(R.id.timesecend);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mMessage.getVoiceLength() / 1000; i++) {
            if (i < 5) {
                sb.append("  ");
            } else {
                sb.append(" ");
            }
        }
        if (ChatHelper.getInstance().getUserInfo().getUid().equals(mMessage.getUid())) {//发出去的
            contentTextView.setText(mMessage.getVoiceLength() / 1000 + "\"" );
        } else {
            contentTextView.setText(mMessage.getVoiceLength() / 1000 + "\"");
        }
        playImage = (ImageView) findViewById(R.id.play);
    }

    //加载网络语音
    private void playRemoteVoice(final String url) {
        File file = FileUtil.getAMR(mContext, url);
        LogCatUtil.e("frank","网络语音 首次获取arm file= "+file);
        if (file != null) {
            LogCatUtil.e("frank","网络语音 获取arm file完成 准备播放"+file);
            VoiceManager.INSTANCE.playVoice(file, VoiceMessageItem.this);
            isPlaying = true;
        } else {
            LogCatUtil.e("frank","网络语音 arm file没有创建 开始创建");
            file = FileUtil.creatAMR(mContext, url);
            if(file!=null && ValidateUtils.isValidate(url)){
                LogCatUtil.e("frank","网络语音 arm file创建完毕 开始下载");
                FileDownload fileDownload = new FileDownload(this, file);
                fileDownload.execute(url);
            }else{
                LogCatUtil.e("frank","网络语音 arm file创建失败");
            }
        }
    }


    @Override
    public void startPlay(MediaPlayer mediaPlayer) {
        isPlaying = true;
        getHandler().post(updateUI);

    }

    @Override
    public void stopPlay(MediaPlayer mediaPlayer) {
        isPlaying = false;
    }

    @Override
    public void onPlayError() {
        playRemoteVoice(mMessage.getContent());
    }


    @Override
    public void startDownload(int length) {

    }

    @Override
    public void downloading(int len) {

    }

    @Override
    public void downLoadEnd(File file) {
        VoiceManager.INSTANCE.playVoice(file, VoiceMessageItem.this);
        isPlaying = true;
    }

    @Override
    public void downLoadError() {
        playRemoteVoice(mMessage.getContent());
    }

    private Runnable updateUI = new Runnable() {
        @Override
        public void run() {
            LogCatUtil.e("zjw","播放声音动画="+targetImage[postion]);
            playImage.setImageResource(targetImage[postion]);
            if (postion < 2) {
                postion++;
            } else {
                postion = 0;
            }
            if (isPlaying) {
                if (getHandler() != null)
                    getHandler().postDelayed(updateUI, 300);
            } else {
                playImage.setImageResource(defaultImage);
            }
        }
    };



}
